<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>修改密码</el-breadcrumb-item>
		</el-breadcrumb>
		<el-form :model="editInfo" label-width="100px">
			<el-form-item label="旧密码">
				<el-input v-model="oldPassword"></el-input>
			</el-form-item>
			<el-form-item label="新密码">
				<el-input v-model="editInfo.rootPassword"></el-input>
			</el-form-item>
			<el-form-item label="确认密码">
				<el-input v-model="confirmPassword"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="clearUserInfo">清空</el-button>
				<el-button type="primary" @click="saveUserInfo">保存</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	import {
		url
	} from '../../util/tool.js'
	export default {
		data() {
			return {
				"oldPassword": "",
				"confirmPassword": "",
				editInfo: { // 个人对象信息
					"id": "",
					"rootName": "",
					"rootPassword": "",
					"rootNickname": "",
					"rootEmail": "",
					"rootPhone": "",
					"rootRole": ""
				},
			}
		},
		methods: {
		
			// 清空个人内容
			clearUserInfo() {
				let id = sessionStorage.getItem('id')
				this.editInfo = {
					"id": id,
					"rootName": "",
					"rootPassword": "",
					"rootNickname": "",
					"rootEmail": "",
					"rootPhone": "",
					"rootRole": ""
				}
				this.oldPassword=''
				this.confirmPassword=''
			},
			//	保存个人内容
			saveUserInfo() {
				let oldPwd = sessionStorage.getItem('password')
				if(oldPwd!=this.oldPassword){
					this.$message.error("旧密码输入错误")
				}else if(this.editInfo.rootPassword!=this.confirmPassword){
					this.$message.error("两次密码输入不一致")
				}else if(this.editInfo.rootPassword.length<6){
					this.$message.error("新密码位数不能少于6位")
				}else{
					this.$http.put("rootUser", this.editInfo)
						.then(res => {
							if (res.data.status == 200) {
								sessionStorage.setItem('password', this.editInfo.rootPassword)
								this.$message.success("修改成功！")
								// this.clearUserInfo()
								this.$router.push('/login')
							} else {
								this.$message.error("修改失败！")
							}
						})
				}
			}
		},
			
			
		mounted() {
			let rid = sessionStorage.getItem('id')
			this.editInfo.id = rid
			// 动态的获取tool.js中的url，并赋值给当前组件的全局url
			this.url = url;
		}
	}
</script>

<style>
</style>